<template>
  <!-- 成果分类 -->
  <a-select v-bind="$attrs" v-on="$listeners" :placeholder="placeholder" v-model="currentValue" allowClear>
    <a-spin v-if="fetching" slot="notFoundContent" size="small" />
    <a-select-option v-else v-for="i in list" :key="i.id" :value="i[replaceFields.key]">{{ i[replaceFields.name] }}</a-select-option>
  </a-select>
</template>
<script>
import { getClassifyList } from '@/api/achievementsType'

export default {
  name:'SelectAchievementType',
  inheritAttrs: false,
  props: {
    // v-model绑定的数据
    value: {
      type: [Array, String],
    },
    // 需要联动的表单名，
    form: {
      type: Object,
      default: function () {
        return {}
      },
    },
    // placeholder
    placeholder:{
      type: String,
      default: function () {
        return '请选择'
      },
    },
    // 是否获取全部列表（包括停用项）
    isAll: {
      type: Boolean,
      default() {
        return true
      },
    },
    replaceFields: {
      type: Object,
      default() {
        return {
          key: 'id',
          name: 'name',
        }
      },
    },
  },
  data() {
    return {
      fetching: false,
      list: [],
    }
  },
  mounted() {
    this.getList()
  },
  methods: {
    getList() {
      this.fetching = true
      let params = {}
      if(!this.isAll){
        params.status = '1'
      }
      getClassifyList(params)
        .then((res) => {
          this.fetching = false
          if (res.code === 200) {
            this.list = res.data
          } else {
            this.$message.error('err:', res.message)
          }
        })
        .catch((res) => {
          this.fetching = false
        })
    },
  },
  computed: {
    // 转换绑定的选中数据，与v-model同步
    currentValue: {
      get: function () {
        return this.value
      },
      set: function (value) {
        this.$emit('input', value)
      },
    },
  }
}
</script>
